/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.view;

import ohos.agp.components.AttrHelper;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.components.DependentLayout;
import ohos.agp.components.DirectionalLayout;
import ohos.agp.components.Image;
import ohos.agp.components.LayoutScatter;
import ohos.agp.components.Text;
import ohos.agp.utils.Color;
import ohos.app.Context;
import ohos.multimodalinput.event.TouchEvent;

import com.xuexiang.xuidemo.ResourceTable;

/**
 * 自定义导航栏页面
 *
 * @since 2021-03-22
 */
public class DirectionLaytionTextview extends DirectionalLayout implements
    Component.ClickedListener, Component.TouchEventListener {
    private Text bacetext;
    private Text textcent;
    private Text textright;
    private Text futext;
    private Image img1;
    private Image img2;
    private DependentLayout dependenBace;
    private DependentLayout depenright;
    private DependentLayout dependentfu;
    //private DependentLayout delayout;
    private DirectionalLayout directLayout;
    private CallbackClick callbackClick;
    //private Context context;
    private static final int anInt1 = 1;
    private static final int anInt2 = 2;
    private static final int anInt3 = 3;
    private static final int anInt4 = 4;
    private static final int anInt15 = 15;
    private static final int anInt20 = 20;

    /**
     * 构造方法
     *
     * @param cont 上下文
     */
    public DirectionLaytionTextview(Context cont) {
        super(cont);
        //this.context = cont;
        initview();
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 1
     */
    public DirectionLaytionTextview(Context context, AttrSet attrSet) {
        super(context, attrSet);
        //this.context = context;
        initview();
    }

    /**
     * 构造方法
     *
     * @param context 上下文
     * @param attrSet 1
     * @param styleName 1
     */
    public DirectionLaytionTextview(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        //this.context = context;
        initview();
    }

    private void initview() {
        LayoutScatter.getInstance(getContext()).parse(ResourceTable.Layout_textview_layout, this, true);
        dependenBace = (DependentLayout) findComponentById(ResourceTable.Id_DependenBace); // 返回的布局
        dependenBace.setClickedListener(this::onClick);
        //delayout = (DependentLayout) findComponentById(ResourceTable.Id_Delayout);
        depenright = (DependentLayout) findComponentById(ResourceTable.Id_Depenright); // 最右边的布局
        directLayout = (DirectionalLayout) findComponentById(ResourceTable.Id_DirectLayout);
        img1 = (Image) findComponentById(ResourceTable.Id_img1);
        img1.setClickedListener(this::onClick);
        img2 = (Image) findComponentById(ResourceTable.Id_img2);
        img2.setClickedListener(this::onClick);
        bacetext = (Text) findComponentById(ResourceTable.Id_bacetext);
        textcent = (Text) findComponentById(ResourceTable.Id_textcent); // 最大的标题
        futext = (Text) findComponentById(ResourceTable.Id_futext);
        dependentfu = (DependentLayout) findComponentById(ResourceTable.Id_Dependentfu);
        textright = (Text) findComponentById(ResourceTable.Id_textright);
        textright.setClickedListener(this::onClick);
        bacetext.setTouchEventListener(this::onTouchEvent);
        textcent.setTouchEventListener(this::onTouchEvent);
        textright.setTouchEventListener(this::onTouchEvent);
    }

    @Override
    public void onClick(Component component) {
        switch (component.getId()) {
            case ResourceTable.Id_img1: // 最左边的图片
            case ResourceTable.Id_DependenBace:
            case ResourceTable.Id_textright: // 最右边的字体
            case ResourceTable.Id_img2: // 最右边的图片
            case ResourceTable.Id_textcent: // 中间的大标题
                callbackClick.setComponent(component);
                break;
            default:
                break;
        }
    }

    /**
     * 设置标题
     *
     * @param title 主标题内容
     * @param fut 副标题内容
     */
    public void settitle(String title, String fut) {
        textcent.setText(title);
        futext.setText(fut);
    }

    /**
     * 设置最左边边的布局方法
     *
     * @param tag 对应的级别
     * @param img 图片
     * 1 只显示文字 2不显示文字,只显示图片
     */
    public void setLeftLayout(int tag, int img) {
        switch (tag) {
            case anInt1:
                bacetext.setText("返回");
                bacetext.setTextSize(AttrHelper.vp2px(anInt15, getContext()));
                break;
            case anInt2:
                bacetext.setVisibility(Component.HIDE);
                directLayout.setMarginLeft(AttrHelper.vp2px(anInt15, getContext()));
                img1.setPixelMap(img);
                break;
            case anInt3:
                dependenBace.setVisibility(Component.HIDE);
                break;
            case anInt4:
                bacetext.setVisibility(Component.VERTICAL);
                directLayout.setMarginLeft(AttrHelper.vp2px(anInt20, getContext()));
                img1.setPixelMap(img);
                break;
            default:
                break;
        }
    }

    /**
     * 设置中间属性的方法
     *
     * @param tag 1和2
     */
    public void setCentent(int tag) {
        switch (tag) {
            case anInt1: // 隐藏副标题,主标题居中
                dependentfu.setVisibility(Component.HIDE);
                break;
            case anInt2:
                textcent.setClickedListener(this::onClick);
                dependentfu.setVisibility(Component.HIDE);
                break;
            default:
                break;
        }
    }

    /**
     * 这是最右边布局的操作方法
     *
     * @param tag tag为1隐藏全部.tag为2,把字体隐藏掉,把图片更换
     * @param img 需要显示的图片
     */
    public void setright(int tag, int img) {
        switch (tag) {
            case anInt1:
                depenright.setVisibility(Component.HIDE);
                break;
            case anInt2:

                // textright.setVisibility(Component.HIDE); 字体本来就是隐藏的
                img2.setPixelMap(img);
                break;
            case anInt3:
                textright.setVisibility(Component.VISIBLE);
                img2.setVisibility(Component.HIDE);
                break;
            default:
                break;
        }
    }

    private void touchText(TouchEvent touchEvent, Component component) {
        Text text = (Text) component;
        switch (touchEvent.getAction()) {
            case TouchEvent.PRIMARY_POINT_DOWN:
                text.setTextColor(new Color(Color.getIntColor("#DCDCDC")));
                break;
            case TouchEvent.CANCEL:
            case TouchEvent.PRIMARY_POINT_UP:
                text.setTextColor(new Color(Color.getIntColor("#FFFFFF")));
                break;
            default:
                break;
        }
    }

    @Override
    public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
        switch (component.getId()) {
            case ResourceTable.Id_bacetext:
            case ResourceTable.Id_textright:
            case ResourceTable.Id_textcent:
                touchText(touchEvent, component);
                break;
            default:
                break;
        }
        return true;
    }

    /**
     * 接口提供外部回调
     *
     * @since 2021-03-30
     */
    public interface CallbackClick {
        /**
         * 接口内部方法
         *
         * @param component
         */
        void setComponent(Component component);
    }

    /**
     * 设置点击事件
     *
     * @param callback
     */
    public void setclick(CallbackClick callback) {
        this.callbackClick = callback;
    }
}
